package com.example.bleuesprit.parkAndroidClient;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.bleuesprit.parkAndroidClient.net.NetUtil;

import org.json.JSONException;
import org.json.JSONObject;


/**
 * Created by bleuesprit on 2015/10/28.
 */



public class ParkDetailActivity extends Activity {


    //timePaker 的返回值

    private int parkID=-1;


    TextView parkName;
    TextView parkTelNumber;
    TextView parkTotalSpace;
    TextView parkEmptySpace;
    TextView parkDescription;
    TextView parkDetailAddress;

    TextView parkMaxOrderTime;
    TextView parkPrice;
    EditText parkOrderNumber;
    EditText parkOrderHour;
    Button orderSpace_Bnt;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0x123) {
                // 注册信息返回结果
                String orderResult="";
                String errorPoint="";
                try {
                    JSONObject jsonObject=new JSONObject(msg.getData().getString("result"));
                    orderResult=jsonObject.getString("orderResult");
                    errorPoint=jsonObject.getString("errorPoint");

                } catch (JSONException e) {
                    e.printStackTrace();
                }


                if(orderResult.equals("succeed")) {
                    Toast.makeText(ParkDetailActivity.this, "order succeed", Toast.LENGTH_LONG).show();
//                    Intent intent=new Intent(ParkDetailActivity.this,LoginActivity.class);
//                    startActivity(intent);
//                    finish();
                }
                else
                {
                    switch (errorPoint)
                    {
                        case "session_userID_noget":
                            Toast.makeText(ParkDetailActivity.this, "获取session失败，请重新登录", Toast.LENGTH_LONG).show();

                            break;
                        case "session_userID_noExist":
                            Toast.makeText(ParkDetailActivity.this, "未知用户名", Toast.LENGTH_LONG).show();
                            break;

                        default:
                            Log.e("ParkDetailActivity", "orderResult errorPoint invalid");
                            break;
                    }
                }
                }
            }

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_park_detail);
        parkName=(TextView)findViewById(R.id.parkDetailParkName);
        parkTelNumber=(TextView)findViewById(R.id.parkDetailTelNumber);
        parkTotalSpace=(TextView)findViewById(R.id.parkDetailTotalSpace);
        parkEmptySpace=(TextView)findViewById(R.id.parkDetailEmptySpace);
        parkDescription=(TextView)findViewById(R.id.parkDetailDescription);
        parkDetailAddress=(TextView)findViewById(R.id.parkDetailAddress);
        parkMaxOrderTime =(TextView)findViewById(R.id.parkDetailMaxOrderTime);
        parkPrice =(TextView)findViewById(R.id.parkDetailPricePerHour);
        parkOrderNumber =(EditText)findViewById(R.id.parkDetailOrderNumber);
        parkOrderHour =(EditText)findViewById(R.id.parkDetailOrderHour);
        orderSpace_Bnt=(Button)findViewById(R.id.parkDetailDoOrderSpace);
        orderSpace_Bnt.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        doOrderSpace(v);
                    }
                }
        );



        Intent intent=getIntent();

        String parkDetailJsonString =intent.getStringExtra("parkDetailJsonString");
        if(parkDetailJsonString==null)
        {
            parkDetailJsonString="";
        }

        try {
            JSONObject parkDetailJson=new JSONObject(parkDetailJsonString);
            parkName.setText(parkDetailJson.getString("name"));
            parkTotalSpace.setText(parkDetailJson.getString("totalSpace"));
            parkEmptySpace.setText(parkDetailJson.getString("emptySpace"));
            parkDescription.setText(parkDetailJson.getString("description"));
            parkDetailAddress.setText(parkDetailJson.getString("address"));
            parkMaxOrderTime.setText(parkDetailJson.getString("maxOrderTime"));
            parkPrice.setText(parkDetailJson.getString("price"));
            parkTelNumber.setText(parkDetailJson.getString("telNumber"));
            parkID=Integer.valueOf(parkDetailJson.getString("uuid"));
        } catch (JSONException e) {
            Log.e("ParkDetailActivity","parse parkDetailJsonString faill");
            e.printStackTrace();
        }


        Toast.makeText(this,parkDetailJsonString,Toast.LENGTH_LONG).show();
    }


    void doOrderSpace(View v)
    {


        JSONObject registMessage=new JSONObject();

        try {
            registMessage.put("parkID",Integer.toString(parkID));
            registMessage.put("orderTime",parkOrderHour.getText().toString());
            registMessage.put("orderNumber",parkOrderNumber.getText().toString());

        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtil.sendToServer("orderSpace", registMessage, handler);

    }

}
